 --
-- Created by IntelliJ IDEA.
-- User: SOSO
-- Date: 14-11-5
-- Time: 下午3:29
-- 可以支持单行 多个（物品x数量）
-- 可以支持多行 带颜色的文字




ColorText = class("ColorText",function()
    local ret = cc.Sprite:create()
    -- 
    ret.node_real = nil
    -- 
    ret.width_total = 0     -- 最终结果的宽度
    ret.height_total = 0    -- 最终结果的高度。
    ret.width = 0   -- 临时宽度。
    ret.height = 0  -- 临时高度。
    ret.lastLabelHeight = 0  -- 上一个label的高度
    ret.startY = 0
    ret.maxWidth = nil 
    ret.maxHeight = nil

    -- 排版类型：居左，居中，居右
    ret.anchorType = 0 
    -- 排版类型: 上边为0点，中点为0点，底边为0点
    ret.anchorHeightType = 0
    --
    ret.ary_lines = {}
    ret.ary_allNode = {}
    ret.ary_allLabel = {}

    return ret
end)

ColorText.__index = ColorText







function ColorText:create()
    local ret = ColorText:new()
    ret:init()
    return ret
end



function ColorText:createWithMaxSize(size)
    local ret = ColorText:new()
    ret.maxWidth = size.width
    ret.maxHeight = size.height
    ret:init()
    return ret
end



function ColorText:addAndReplaceNode(node)
    -- zjb 2015年4月30日 修改
    replaceNode(self,node)
    return self
end


















---------------------------------------------------------------
--      init
---------------------------------------------------------------
function ColorText:init()
    self:setAnchorPoint(cc.p(0,0))
    
    self.node_real = cc.Sprite:create()
    self.node_real:setAnchorPoint(cc.p(0,0))
    self:addChild(self.node_real)
end



local FontStyle = {
    [0] = { 0x007f,0x007f,0x007f,0x00bf,0x00bf,0x00bf,2,0,0 },
    [1] = { 0x00ff,0x00ff,0x00ff,0x005b,0x0047,0x0030,2,0,0 },
    [2] = { 0x00fe,0x00e9,0x00cf,0x005b,0x0047,0x0030,2,0,0 },
    [3] = { 0x00ff,0x00cc,0x0000,0x005b,0x0047,0x0030,2,0,0 },
    [4] = { 0x00ff,0x00c6,0x005e,0x005b,0x0047,0x0030,2,0,0 },
    [5] = { 0x00ff,0x009c,0x0000,0x005b,0x0047,0x0030,2,0,0 },
    [6] = { 0x0000,0x00ff,0x0011,0x005b,0x0047,0x0030,2,0,0 },
    [7] = { 0x00ff,0x000f,0x000f,0x005b,0x0047,0x0030,2,0,0 },
    [8] = { 0x00ff,0x00d8,0x0000,0x00bb,0x0026,0x0026,2,0,0 },
    [9] = { 0x006c,0x002a,0x0006,0x00ff,0x00b3,0x003f,2,0,0 },
    [10] = { 0x006c,0x002a,0x0006,0x00ff,0x0073,0x004e,2,0,0 },
    [11] = { 0x00ff,0x00df,0x00c6,0x00e0,0x004c,0x000a,2,0,0 },
    [12] = { 0x00ff,0x00ff,0x00ff,0x00f3,0x005d,0x0032,2,0,0 },
}
function ColorText:setLabelStyle(label,nStyle)
    label:setStyle(nStyle)
--[[-  根据c++的来
    if nStyle >= 1 and nStyle <= 12 then
        local pFontStyle = FontStyle[nStyle]
        local r = pFontStyle[1]
        local g = pFontStyle[2]
        local b = pFontStyle[3]
        local r2 = pFontStyle[4]
        local g2 = pFontStyle[5]
        local b2 = pFontStyle[6]
        local strokeSize = pFontStyle[7]
        local shadowDistance = pFontStyle[8]
        local shadowBlur = pFontStyle[9]
        label:setColor(cc.c3b(255,255,255))

        if shadowDistance ~= 0 then
            label:setTextColor(cc.c4b(r,g,b,255))
            label:enableShadow(cc.c4b(r2,g2,b2,255),cc.size(shadowDistance,shadowDistance),shadowBlur)
        end
        if strokeSize ~= 0 then
            label:setTextColor(cc.c4b(r,g,b,255));
            label:enableOutline(cc.c4b(r2,g2,b2,255),strokeSize)
        end
        if shadowDistance == 0 and strokeSize == 0 then
            label:setColor(cc.c3b(r,g,b))
        end
    end  
    --]] 
end

















---------------------------------------------------------------
--      栋爷用， 单行：加载 item，图标，加文字
---------------------------------------------------------------
local dic_path = {  [0] =  "res/public/icon/img_gold.png", -- "res/public/img_small_gold.png", -- 
                    [-1] = "res/public/img_small_diamond.png",
                    [-2] = "res/task/img_exp.png",
                    [-3] = "res/public/img_small_drumstick.png",
                    [-4] = "res/public/img_sports_currency.png",
                    [-5] = "res/public/img_passthrough_currency.png",
                    [-6] = "res/public/img_society_currency.png",
                    
                    [-7] = "res/public/icon/img_diamond.png",-- 新版钻石 
                    [-8] = "res/public/icon/img_soul.png",-- 新版灵魂石
                    }


function ColorText:insertItemIdAndCount(nItemId,nCount)
    -- 新需求，领奖有英雄， -108这种，需要加英雄展示界面.
    if nItemId<-8 then nItemId=0 end
    
    if nItemId<1 then
        local sp = cc.Sprite:createWithSpriteFrameName(dic_path[nItemId])
        local spSize = sp:getBoundingBox()
        local scale = 1
        if spSize.height>self.maxHeight then 
            scale = self.maxHeight/spSize.height    
        elseif spSize.width>self.maxHeight then 
            scale = self.maxHeight/spSize.width
        else
            -- scale = self.maxHeight/spSize.height
        end
        sp:setScale(scale)
        sp:setPosition(cc.p(spSize.width/2*scale + self.width,spSize.height/2*scale))
        self.node_real:addChild(sp)
        table.insert(self.ary_allNode,sp)
        self.width = self.width + spSize.width*scale
    else
        local item = Item:create()
        item:setItemId(nItemId)
        local scale = self.maxHeight/item.size_icon.width
        item:setScale(scale)
        item:setPosition(cc.p(item.size_icon.width/2*scale + self.width,item.size_icon.height/2*scale))
        self.node_real:addChild(item)
        table.insert(self.ary_allNode,item)
        self.width = self.width + item.size_icon.width*scale
    end

    if nCount then 
        local lab = CLabel:createWithTTF(" x"..nCount,DEFAULT_TTF_PATH,20)
        self:setLabelStyle(lab,1)
        lab:setAnchorPoint(0,0)
        lab:setHorizontalAlignment(0)
        lab:setPosition(cc.p(self.width,0))
        self.node_real:addChild(lab)
        table.insert(self.ary_allLabel,lab)
        local width_lab = lab:getBoundingBox().width
        self.width = self.width + width_lab
    end

    -- 加10的隔离空间
    self.width = self.width + 10
    -- 更新外部要用的宽度参数:
    if self.width>self.width_total then  self.width_total=self.width end
    -- 让内板水平居中居左或居右。
    self:resetAnchor()
    -- 让内板竖直方向居中
    self.node_real:setPosition(cc.p(0,-self.maxHeight/2))
end



function ColorText:setItemLabelStyle(nStyle)
    for k,lab in pairs(self.ary_allLabel)do 
        self:setLabelStyle(lab,nStyle)
    end
end



function ColorText:clear()
    for k,node in pairs(self.ary_allNode)do
        node:removeFromParent()
    end
    for k,lab in pairs(self.ary_allLabel)do 
        lab:removeFromParent()
    end
    self.ary_allNode = {}
    self.ary_allLabel = {}
    self.width = 0
    self.height = 0
end








---------------------------------------------------------------
--      属性文字用: 设置居中居左居右属性。
---------------------------------------------------------------
function ColorText:setAnchorCenter()
    self.anchorType = 1
    self:resetAnchor()
end

function ColorText:setAnchorLeft()
    self.anchorType = 0
    self:resetAnchor()
end

function ColorText:setAnchorRight()
    self.anchorType = 2
    self:resetAnchor()
end

function ColorText:setAnchorHeightTop()
    self.anchorHeightType = 0
    for _,lab in pairs(self.ary_allLabel)do 
        -- lab:setVerticalAlignment(self.anchorHeightType)
        lab:setAnchorPoint(0,1)
    end
    self:resetAnchor()
end

function ColorText:setAnchorHeightCenter()
    self.anchorHeightType = 1
    for _,lab in pairs(self.ary_allLabel)do 
        -- lab:setVerticalAlignment(self.anchorHeightType)
        lab:setAnchorPoint(0,0.5)
    end
    self:resetAnchor()
end

function ColorText:setAnchorHeightBottom()
    self.anchorHeightType = 2
    for _,lab in pairs(self.ary_allLabel)do 
        -- lab:setVerticalAlignment(self.anchorHeightType)
        lab:setAnchorPoint(0,0)
    end
    self:resetAnchor()
end


function ColorText:resetAnchor()
    local xx,yy = self.node_real:getPosition()
    if self.anchorType==0 then -- 居左
        xx = 0
    elseif self.anchorType==1 then -- 水平居中
        xx = -self.width_total/2
    elseif self.anchorType==2 then -- 水平居右
        xx = -self.width_total
    end
    if self.anchorHeightType==0 then -- 上
        yy=0
    elseif self.anchorHeightType==1 then -- 中
        yy=self.height_total/2 
    elseif self.anchorHeightType==2 then -- 底
        yy=self.height_total
    end
    self.node_real:setPosition(cc.p(xx,yy))
end










---------------------------------------------------------------
--      属性文字用: 设置同行不同的文字，并能设置多行
---------------------------------------------------------------
function ColorText:insertTextAndStyle(sText,nStyle,nFontSize)
    nFontSize = nFontSize or 20
    local lab = CLabel:createWithTTF(sText,DEFAULT_TTF_PATH,nFontSize)
    self:setLabelStyle(lab,nStyle)
    lab:setAnchorPoint(0,1)
    lab:setHorizontalAlignment(0)
    lab:setVerticalAlignment(self.anchorHeightType)-- 居顶，居中，居底
    lab:setPosition(cc.p(self.width,self.startY))
    self.node_real:addChild(lab)
    local size_lab = lab:getBoundingBox()
    self.width = self.width + size_lab.width
    self.lastLabelHeight = size_lab.height+nFontSize/5     -- 行距+5的像素
    -- 记录所添加的label
    table.insert(self.ary_allNode,lab)
    table.insert(self.ary_allLabel,lab)
    -- 更新外部要用的宽度参数:
    if self.width>self.width_total then  self.width_total=self.width end
    self:resetAnchor()
end



-- 现在只支持向下换行。
function ColorText:nextLine()
    self.sNextLineDirect = "down"
    if self.width==0 then return end
    self.width = 0
    local xx,yy = self.node_real:getPosition()
    -- self.node_real:setPosition(cc.p(xx,self.height))
    self.height = self.height+self.lastLabelHeight
    self.startY = self.startY-self.lastLabelHeight
    self.height_total = self.height
    cclook("当前高度: %s",self.height_total)
    self:resetAnchor()
end








